﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quality;
namespace Quality.Records
{
    public partial class manage : Form
    {
        public manage()
        {
            InitializeComponent();
            InitColumnInfo();
            this.dgInfo.AutoGenerateColumns = false;
            DB.getcombobox(shift, "shift", "num", "shiftname");
            DB.getcombobox(typenum, "testtypes", "num", "typename");
            DB.getcombobox(worknum, "workcenters", "num", "workname");
            
           
        }private void cbHeader_OnCheckBoxClicked(bool state)
        {
            //这一句很重要结束编辑状态
            dgInfo.EndEdit();
            dgInfo.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
        }

       
        private void InitColumnInfo()
        {
            int index = 0;

            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
            DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
            colCB.HeaderCell = cbHeader;
            colCB.HeaderText = "全选";
            cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
            dgInfo.Columns.Insert(index, colCB);

            index++;
            dgInfo.Columns[index].HeaderText = "单号";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "行号";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "生产日期";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "班次";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "成品编码";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "成品名称";
            index++;
            dgInfo.Columns[index].HeaderText = "批次";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "工作中心";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "设备";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "检验类型";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "检验项目";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "检验指标";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "标准值";
            dgInfo.Columns[index].Width = 90; 
            index++;
            dgInfo.Columns[index].HeaderText = "实际值";
            dgInfo.Columns[index].Width = 90; 
            index++;
            dgInfo.Columns[index].HeaderText = "创建人";
            dgInfo.Columns[index].Width = 90; 
            index++;
            dgInfo.Columns[index].HeaderText = "创建日期";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "审核人";
            dgInfo.Columns[index].Width = 90;
            index++;
            dgInfo.Columns[index].HeaderText = "审核日期";
            dgInfo.Columns[index].Width = 90;


           
           

            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Blue;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Blue;
            dgInfo.Columns[index].DefaultCellStyle = dataGridViewCellStyle2;
        }
        private void q()
        {
            if (string.IsNullOrEmpty(workdate.Text) || string.IsNullOrEmpty(workdate1.Text))
            {
                MessageBox.Show("生产日期不能为空！");
            }

            string s = "";

            if (!string.IsNullOrEmpty(num.Text))
            {
                s = s + " and a.num='" + num.Text.Trim() + "' ";

            }
            if (!string.IsNullOrEmpty(shift.Text))
            {
                s = s + " and a.shift='" + shift.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(pronum.Text))
            {
                s = s + " and a.pronum like '%" + pronum.Text.Trim() + "%' ";

            }
            if (!string.IsNullOrEmpty(proname.Text))
            {
                s = s + " and a.proname like '%" + proname.Text.Trim() + "%' ";

            }

            if (!string.IsNullOrEmpty(batch.Text))
            {
                s = s + " and a.batch like '%" + batch.Text.Trim() + "%' ";

            }
            if (!string.IsNullOrEmpty(typenum.Text))
            {
                s = s + " and a.typenum='" + typenum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(itemnum.Text))
            {
                s = s + " and a.itemnum='" + itemnum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(indnum.Text))
            {
                s = s + " and a.indnum='" + indnum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(eqnum.Text))
            {
                s = s + " and a.eqnum='" + eqnum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(worknum.Text))
            {
                s = s + " and c.num='" + worknum.SelectedValue + "' ";

            }
            if (!string.IsNullOrEmpty(comboBox1.Text))
            {
                if (comboBox1.Text == "审核") { 
                s = s + " and a.audit is not null";
                }
                if (comboBox1.Text == "未审核")
                {
                    s = s + " and a.audit is  null";
                }
            }

            DataTable dt = sqlHelper.ExecuteDataTable("select a.num,a.rownum,a.workdate,a.shift,a.pronum ,a.proname,a.batch,c.workname,b.eqname,d.typename,e.itemname,f.indname,f.indvalue,a.testvalue ,a.making,a.makingdate,a.audit,a.auditdate from records a left join equipments b on a.eqnum=b.num left join workcenters c on b.worknum=c.num  left join testtypes d on a.typenum=d.num  left join testitems e on a.itemnum=e.num left join  testindicators f on a.indnum=f.num where workdate>='" + workdate.Value + "' and workdate<='" + workdate1.Value + "' " + s + "");

            dgInfo.DataSource = dt;
          
        }
        private void button3_Click(object sender, EventArgs e)
        {
            q();
            
        }

        private void typenum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (typenum.SelectedIndex != -1) {
                DataTable dt = sqlHelper.ExecuteDataTable("select num,itemname from testitems a left join assigneditems b on a.num=b.itemnum where b.typenum='"+typenum.SelectedValue+"'");
                itemnum.DataSource = dt;
                itemnum.DisplayMember = "itemname";
                itemnum.ValueMember = "num";
                itemnum.SelectedIndex = -1;
            
            }
        }

        private void itemnum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (itemnum.SelectedIndex != -1)
            {
                DataTable dt = sqlHelper.ExecuteDataTable("select num,indname from testindicators a left join assignedindicators b on a.num=b.indnum where b.itemnum='" + itemnum.SelectedValue + "'");
                indnum.DataSource = dt;
                indnum.DisplayMember = "indname";
                indnum.ValueMember = "num";
                indnum.SelectedIndex = -1;

            }

        }

        private void worknum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (worknum.SelectedIndex != -1)
            {
               
                DataTable dt = sqlHelper.ExecuteDataTable("select num,eqname from equipments  where worknum='" + worknum.SelectedValue + "'");
                eqnum.DataSource = dt;
                eqnum.DisplayMember = "eqname";
                eqnum.ValueMember = "num";
                 eqnum.SelectedIndex = -1;
            }
        }

        private void audit_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgr in dgInfo.Rows)
            {
                Boolean flag = Convert.ToBoolean(dgr.Cells[0].Value);
                if (flag)
                {
                    sqlHelper.ExecuteNonQuery("update records set audit='" + sqlHelper.loginname + "',auditdate='" + DateTime.Now.ToString() + "' where rownum ='" + dgr.Cells[2].Value + "'");

                }
            }
            MessageBox.Show("审核成功！");
            q();
        }

        private void unaudit_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dgr in dgInfo.Rows)
            {
                Boolean flag = Convert.ToBoolean(dgr.Cells[0].Value);
                if (flag)
                {
                    sqlHelper.ExecuteNonQuery("update records set audit=NULL,auditdate=NULL where rownum ='" + dgr.Cells[2].Value + "'");

                }
            }
            MessageBox.Show("弃审成功！");
            q();
        }

        private void delete_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除吗?", "删除", messButton);
            if (dr == DialogResult.OK)
            {
                foreach (DataGridViewRow dgr in dgInfo.Rows)
                {
                    Boolean flag = Convert.ToBoolean(dgr.Cells[0].Value);
                    if (flag)
                    {
                        sqlHelper.ExecuteNonQuery("Delete from records where rownum ='" + dgr.Cells[2].Value + "'");

                    }
                }
                
                q();
            }
        }
    
    }
}
